package com.git.hui.yinshua.biz.rest.admin.tools;

import com.alibaba.fastjson.JSONObject;
import com.git.hui.yinshua.api.model.ResVo;
import com.git.hui.yinshua.api.model.permission.Permission;
import com.git.hui.yinshua.api.model.permission.UserAccessRole;
import com.git.hui.yinshua.biz.order.service.PtOrderService;
import com.git.hui.yinshua.biz.pay.service.PaymentService;
import com.git.hui.yinshua.biz.pay.service.RefundService;
import com.git.hui.yinshua.common.DateUtil;
import com.wechat.pay.java.service.payments.model.Transaction;
import com.wechat.pay.java.service.refund.model.RefundNotification;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.UUID;

/**
 * @author YiHui
 * @date 2024/9/23
 */
@RestController
@RequestMapping(path = "dev")
public class DevController {
    @Autowired
    private PaymentService paymentService;
    @Autowired
    private RefundService refundService;
    @Autowired
    private PtOrderService ptOrderService;

    /**
     * 用于测试支付回调，直接将支付单状态更新为成功
     *
     * @param orderId
     * @return
     */
    @Permission(role = UserAccessRole.ADMIN)
    @GetMapping(path = "testPayStatus")
    public ResVo<Boolean> payCallbackTest(Long orderId, Transaction.TradeStateEnum state) {
        Transaction transaction = new Transaction();
        transaction.setTransactionId("test-" + UUID.randomUUID());
        transaction.setOutTradeNo(String.valueOf(orderId));
        transaction.setTradeState(state);
        transaction.setSuccessTime(state == Transaction.TradeStateEnum.SUCCESS ? DateUtil.WX_PAY_FORMATTER.format(LocalDateTime.now()) : "");
        return ResVo.ok(paymentService.wxPayCallback(transaction));
    }

    /**
     * 模拟微信重新请求
     *
     * @param txt
     */
    @Permission(role = UserAccessRole.ADMIN)
    @GetMapping(path = "wxPayRecall")
    public ResVo<Boolean> testPayNotify(String txt) {
        Transaction transaction = JSONObject.parseObject(txt, Transaction.class);
        boolean ans = paymentService.wxPayCallback(transaction);
        return ResVo.ok(ans);
    }

    /**
     * 模拟退款回调请求
     *
     * @param txt
     * @return
     */
    @Permission(role = UserAccessRole.ADMIN)
    @GetMapping(path = "wxRefundRecall")
    public ResVo<Boolean> testRefundNotify(String txt) {
        RefundNotification notification = JSONObject.parseObject(txt, RefundNotification.class);
        boolean ans = refundService.refundCallback(notification);
        return ResVo.ok(ans);
    }

    /**
     * 模拟退款发起请求 -- 适用于微信接口调用成功，但是内部数据状态不一致的场景
     *
     * @param orderId
     * @param notes
     * @param txt
     * @return
     */
    @GetMapping(path = "wxRefundApply")
    @Permission(role = UserAccessRole.SUPER_ADMIN)
    public ResVo<Boolean> adminRefundTest(Long orderId, String notes, String txt) {
        return ResVo.ok(refundService.adminRefunding(orderId, notes, txt));
    }


    /**
     * 标记拼团单成功
     *
     * @return
     */
    @GetMapping(path = "ptStatusCheck")
    @Permission(role = UserAccessRole.SUPER_ADMIN)
    public ResVo<Boolean> markPtOrderSucceed(Integer ptOrderId) {
        return ResVo.ok(ptOrderService.autoCheckPtOrder(ptOrderId));
    }
}
